package com.adcc.loadsheet.monitor.service;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.monitor.congifuration.RestartServiceConfiguration;
import com.adcc.loadsheet.monitor.util.Constant;
import com.adcc.loadsheet.monitor.util.AlarmEnum;
import com.adcc.loadsheet.monitor.util.RestClient;
import com.adcc.loadsheet.monitor.vo.ResultStatus;
import com.adcc.loadsheet.monitor.vo.ResultVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RestartServiceImpl implements RestartService {

    @Autowired
    private RestartServiceConfiguration restartServiceConfiguration;

    /**
     * 重启服务
     *
     * @param serverName 服务名
     * @return
     */
    @Override
    public ResultVO serviceRestart(String serverName) {
        //返回值
        ResultVO result = new ResultVO();
        //记录日志所需的信息
        AlarmEnum alarmEnum = null;
        //远程重启的url
        String restartAgentUrl = null;
        try {
            //根据请求匹配对应的重启项目
            if (AlarmEnum.MODULE_BGS.equels(serverName)) {
                //匹配BGS服务
                alarmEnum = AlarmEnum.MODULE_BGS;
                restartAgentUrl = restartServiceConfiguration.getBgsGw();
            } else if (AlarmEnum.BGS_MQ.equels(serverName)) {
                //匹配BGS-MQ服务
                alarmEnum = AlarmEnum.BGS_MQ;
                restartAgentUrl = restartServiceConfiguration.getBgsMq();
            } else if (AlarmEnum.WEBSITE1.equels(serverName)) {
                //匹配地服网站1服务
                alarmEnum = AlarmEnum.WEBSITE1;
                restartAgentUrl = restartServiceConfiguration.getWebside1();
            } else if (AlarmEnum.WEBSITE2.equels(serverName)) {
                //匹配地服网站2服务
                alarmEnum = AlarmEnum.WEBSITE2;
                restartAgentUrl = restartServiceConfiguration.getWebside2();
            } else if (AlarmEnum.MODULE_APP_UPLOAD.equels(serverName)) {
                //匹配AppUpLoad服务
                alarmEnum = AlarmEnum.MODULE_APP_UPLOAD;
                restartAgentUrl = restartServiceConfiguration.getAppupload();
            } else if (AlarmEnum.MODULE_MSG_UP_CENTER.equels(serverName)) {
                //匹配MsgUpCenter服务
                alarmEnum = AlarmEnum.MODULE_MSG_UP_CENTER;
                restartAgentUrl = restartServiceConfiguration.getMsgupcenter();
            } else if (AlarmEnum.FORWARDER_FIRSTPORGRAMUP.equels(serverName)) {
                //匹配舱单抓取程序1服务
                alarmEnum = AlarmEnum.FORWARDER_FIRSTPORGRAMUP;
                restartAgentUrl = restartServiceConfiguration.getForwarder1();
            } else if (AlarmEnum.FORWARDER_SECONDPORGRAMUP.equels(serverName)) {
                //匹配舱单抓取程序2服务
                alarmEnum = AlarmEnum.FORWARDER_SECONDPORGRAMUP;
                restartAgentUrl = restartServiceConfiguration.getForwarder2();
            } else if (AlarmEnum.MODULE_MQ.equels(serverName)) {
                //匹配MQ服务
                alarmEnum = AlarmEnum.MODULE_MQ;
                restartAgentUrl = restartServiceConfiguration.getLocalmq();
            }
            //判断配置agentUrl是否为空
            if (Strings.isNullOrEmpty(restartAgentUrl)) {
                result.setStatus(ResultStatus.FAILED);
                result.getData().put("reason", String.format(Constant.RESTART_ERROR, serverName, Constant.RESTART_URL_ISNULL));
                result.getData().put("englishreason", String.format(Constant.RESTART_ERROR_EN, serverName, serverName, Constant.RESTART_URL_ISNULL_EN));
                return result;
            }
            //发送请求重启服务
            String response = RestClient.longGet(restartAgentUrl);
            if (response==null){
                result.setStatus(ResultStatus.FAILED);
                result.getData().put("reason", String.format(Constant.RESTART_ERROR, serverName, Constant.RESTART_ERROR_REQUEST));
                result.getData().put("englishreason", String.format(Constant.RESTART_ERROR_EN, serverName, serverName, Constant.RESTART_ERROR_REQUEST_EN));
                return result;
            }
            //解析响应结果
            result = parseResponse(response, alarmEnum);
        } catch (Exception ex) {
            Log.error(RestartService.class, "serviceRestart() error", ex);
            result.setStatus(ResultStatus.FAILED);
            result.getData().put("reason", String.format(Constant.RESTART_ERROR, serverName, ex.getMessage()));
            result.getData().put("englishreason", String.format(Constant.RESTART_ERROR_EN, serverName, serverName, ex.getMessage()));
        }
        return result;
    }

    /**
     * 解析重启请求的响应结果，status为0重启成功，status为1重启失败
     *
     * @param res  响应结果
     * @param alarmEnum  服务对应枚举
     * @return
     */
    public ResultVO parseResponse(String res, AlarmEnum alarmEnum) {
        ResultVO result = new ResultVO();
        try {
            if (!Strings.isNullOrEmpty(res)) {
                JSONObject obj = JSON.parseObject(res);
                if (obj.containsKey("status")) {
                    if ("0".equals(obj.getString("status"))) {
                        result.setStatus(ResultStatus.SUCCESS);
                        result.getData().put("reason", String.format(Constant.RESTART_SUCCESS, alarmEnum.getEnglishCommand()));
                        result.getData().put("englishreason", String.format(Constant.RESTART_SUCCESS_EN, alarmEnum.getEnglishCommand(), alarmEnum.getEnglishCommand()));
                    } else {
                        result.setStatus(ResultStatus.FAILED);
                        result.getData().put("reason", String.format(Constant.RESTART_ERROR, alarmEnum.getEnglishCommand(), obj.getString("errorReason")));
                        result.getData().put("englishreason", String.format(Constant.RESTART_ERROR_EN, alarmEnum.getEnglishCommand(), alarmEnum.getEnglishCommand(), obj.getString("errorReason")));
                    }
                }
            }
        } catch (Exception ex) {
            //解析失败
            Log.error(RestartService.class, "parseResponse() error", ex);
            result.setStatus(ResultStatus.FAILED);
            result.getData().put("reason", String.format(Constant.RESTART_ERROR, alarmEnum.getEnglishCommand(), Constant.RESTART_ERROR_ANALYSIS));
            result.getData().put("englishreason", String.format(Constant.RESTART_ERROR_EN, alarmEnum.getEnglishCommand(), alarmEnum.getEnglishCommand(), Constant.RESTART_ERROR_ANALYSIS_EN));
        }
        return result;
    }
}
